window.onload = function() {
    var regtel = /^1[3|4|5|7|8]\d{9}$/; // 手机号码的正则表达式
    // var regqq = /^[1-9]\d{4,}$/; // 10000
    // var regnc = /^[\u4e00-\u9fa5]{2,8}$/;
    var regnc = /^[a-zA-Z0-9]{2,8}$/;
    // var regmsg = /^\d{6}$/;
    var regmsg = /^\d{4}$/;
    var regpwd = /^[a-zA-Z0-9_-]{6,16}$/;
    var tel = document.querySelector('#tel');
    // var qq = document.querySelector('#qq');
    var nc = document.querySelector('#nc');
    var msg = document.querySelector('#msg');
    var pwd = document.querySelector('#pwd');
    var surepwd = document.querySelector('#surepwd');
    regexp(tel, regtel); // 手机号码
    // regexp(qq, regqq); // qq号码
    regexp(nc, regnc); // 昵称
    regexp(msg, regmsg); // 短信验证
    regexp(pwd, regpwd); // 密码框
    // 表单验证的函数
    function regexp(ele, reg) {
        ele.onblur = function() {
            if (reg.test(this.value)) {
                // console.log('正确的');
                this.nextElementSibling.className = 'success';
                this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
            } else {
                // console.log('不正确');
                this.nextElementSibling.className = 'error';
                this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确，请从新输入 ';
            }
            // 判断该input框是否是电话号码 如果是电话号码 需要控制 发送验证码按钮的隐藏和显示
            if(this.id == 'tel'){
                if(reg.test(this.value)){
                    // 正则校验通过
                    $('.over2').show();
                }else{
                    // 正则校验不通过
                    $('.over2').hide()
                }
            }
        }
    };

    surepwd.onblur = function() {
        if (this.value == pwd.value) {
            this.nextElementSibling.className = 'success';
            this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
        } else {
            this.nextElementSibling.className = 'error';
            this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 两次密码输入不一致';

        }
    }

    $('.over2').click(function (){
        // console.log(111)
        $.getJSON("/api/sms/sendVerifyCode",
            {phone:$('#tel').val()}, ret=>{
            console.log(ret);
            $('input[name=uuid]').val(ret['data']['uuid']);
            // 控制验证码按钮的倒计时
            let allTime = 40;
            let fun = ()=>{
                let strContent =  ''
                if(allTime > 0){
                    allTime--;
                    strContent += `剩余${allTime}秒`
                    $('.over2').prop('disabled','disabled')
                    $('.over2').css('background','#DEDEE2')
                }else{
                    strContent += '发送验证码'
                    $('.over2').removeAttr("disabled")
                    $('.over2').css('background','#c81623')
                }
                $('.over2').val(strContent)
            }
            fun()
            setInterval(fun,1000)
        })
    })

    // 注册事件
    $('#regForm').submit(()=>{
        let uuid = $('input[name=uuid]').val();
        let phone = $('#tel').val();
        let username = $('#nc').val();
        let code = $('#msg').val();
        let password = $('#pwd').val();
        password = md5(password)
        let jsonReg = JSON.stringify({uuid,phone,username,code,password})
        console.log(jsonReg)
        if($('#agree').prop('checked')){
            // 选中同意
            $.ajax({
                url:'/api/user/reg',
                type:'post',
                dataType:'json',
                contentType: 'application/json;charset=utf-8',
                data:jsonReg,
                success: ret=>{
                    console.log(ret)
                    if(ret['code']==1003){
                        window.location.href = '/login.html'
                    }
                }
            })
            return false
        }else{
            // 没有同意
            alert("请同意协议");
            return false
        }
        return false;
    })

}